﻿//编辑对话框
var dlg = new MyAppDialog(900, 600);

//列定义
var cols = [[
    { field: "Id", title: 'Id', width: 10, hidden: true },
    { field: "ck", title: '', width: 20, checkbox: true },
    { field: "Username", title: '用户名', sortable: true, width: 100, align: 'left' },
    { field: "Name", title: '姓名', sortable: true, width: 100, align: 'left' },
    { field: "Membertype", title: '申请者身份', sortable: true, width: 100, align: 'left' },
    { field: "School", title: '学校名称', sortable: true, width: 100, align: 'left' },
    { field: "Specialty", title: '所属院系', sortable: true, width: 100, align: 'left' },
    { field: "Email", title: '邮箱', sortable: true, width: 150, align: 'left' },
    { field: "Address", title: '地址', sortable: true, width: 150, align: 'left' },
    {
        field: "Islocked", title: '状态', sortable: true, width: 40, align: 'center', formatter: function (value, rec) {
            if (value == "1")
                return "<div class='helper-font-16'><i title='停用' class='iconfont icon-close1' style='color:red;'></i></div>";
            else
                return "<div class='helper-font-16'><i title='可用' class='iconfont icon-check1' style='color:green;'></i></div>";
        }
    },
    { field: "Registerdate", title: '注册时间', sortable: true, width: 130, align: 'left' },
    { field: "Lastlogindate", title: '最后登录', sortable: true, width: 130, align: 'left' },
    //{ field: "Logintimes", title: '登录次数', sortable: true, width: 60, align: 'center', formatter: function (value, rec) { if (value == "0") return ""; else return "<a href='#none'>" + value + "</a>"; } },
    {
        field: "op", title: '操作', width: 200, align: 'center', formatter: function (value, rec) {
            var op = "<div class='btn-group'>";
            if (rec.Islocked == "1") {
                op += "<a href='#none' disabled='disabled' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>禁用</a>"
                op += "<a href='#none' onclick='IslockedFun(this,\"open\")' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>启用</a>"
            }
            else {
                op += "<a href='#none' onclick='IslockedFun(this,\"lock\")' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>禁用</a>"
                op += "<a href='#none' disabled='disabled' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>启用</a>"
            }
            op += "<a href='#none' onclick='retsetFun(this)' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>重置密码</a>"
            op += "</div>";
            return op;
        }
    }
]];


$(document).ready(function () {

    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.condjson = JSON.stringify(getSearchCond());
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });
    //导出
    $("#btnexport").click(function (evt) {
        evt.preventDefault();

        exportFile();
    });

    LoadDatagrid();

    if (!canEdit) {
        $("#datagrid").datagrid('hideColumn', 'op');
    }
});

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

function getSearchCond() {
    var condjson = { Id: 0, Values: {} };

    //条件值
    $(".condfield").each(function () {
        var fieldname = $(this).attr("data-fieldname");
        condjson.Values[fieldname] = $("[data-fieldname=" + fieldname + "]").val();
    });

    return condjson;
}

//导出
function exportFile() {
    var json = JSON.stringify(getSearchCond());
    var sort = $("#datagrid").datagrid('options').sortName;
    var order = $("#datagrid").datagrid('options').sortOrder;
    layer.msg("正在生成文件，请稍后...", { icon: 16, time: 0 });
    $.ajax({
        type: "post",
        url: baseurl + "Member/CreateExportFile",
        dataType: 'json',
        data: {
            condjson: json,
            sort: sort,
            order: order
        },
        success: function (e) {
            layer.closeAll();
            if (e.Ret == "0") {
                location.href = baseurl + "Member/DownFile?key=" + e.Content;
            } else {
                $.messager.alert("错误", e.Msg, "error");
            }
        },
        error: function (xhr) {
            showError(xhr);
        }
    });
}

//加载列表数据
function LoadDatagrid() {
    var condjson = JSON.stringify(getSearchCond());

    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '申请者列表',
        url: baseurl + 'Member/MemberListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            condjson: condjson
        },
        sortName: 'Id',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            if (canEdit) {
                dlg.setSize(900, 600);
                dlg.setURL("查看/编辑申请者", baseurl + "Member/Edit/" + row.Id + "?" + Math.random());
                dlg.show();
            }
        },
        rowStyler: function (rowIndex, row) {
            if (row.Islocked == 1) {
                return "color:#999;";
            }
        }
    });
    //PaginationInit();
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert("提示", "请选择需要删除的数据！");
        return;
    }
    $.messager.confirm("确认", "确认要删除选择的申请者吗？", function (r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < row.length; i++) {
                ids += row[i].Id + ",";
            }

            $.ajax({
                type: "post",
                url: baseurl + "Member/Delete",
                data: { ids: ids },
                success: function (e) {
                    if (e.Ret == "0") {
                        $("#datagrid").datagrid("reload");
                    } else {
                        $.messager.alert("错误", e.Msg, "error");
                    }
                }
            });
        }
    })

}

//重置密码
function retsetFun(obj) {
    dlg.setSize(500, 320);
    dlg.setURL("重置密码", baseurl + "Member/Resetpwd/" + $(obj).attr("data-id") + "?" + Math.random());
    dlg.show();
}

//锁定//解锁
function IslockedFun(obj, sign) {
    if (sign == "lock") {
        $.messager.confirm("确认", "确认要禁用此账号吗？", function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: baseurl + "Member/Islockedpwd" + "?" + Math.random(),
                    data: { id: $(obj).attr("data-id") },
                    success: function (e) {
                        if (e.Ret == "0") {
                            $("#datagrid").datagrid("reload");
                        } else {
                            $.messager.alert("错误", e.Msg);
                        }
                    }
                });
            }
        });
    }
    else {
        $.messager.confirm("确认", "确认要启用此账号吗？", function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: baseurl + "Member/Islockedpwd" + "?" + Math.random(),
                    data: { id: $(obj).attr("data-id") },
                    success: function (e) {
                        if (e.Ret == "0") {
                            $("#datagrid").datagrid("reload");
                        } else {
                            $.messager.alert("错误", e.Msg, "error");
                        }
                    }
                });
            }
        });
    }
}



